//插件
const express = require("express");
const app = express();
const cors = require('cors'); //跨域
const bodyParser = require('body-parser'); //解析JSON
const session = require('express-session'); // session 中间件
const timeout = require("connect-timeout");//超时
//路由
const user = require('./views/user/index');//用户信息
const register = require('./views/register/index');//注册
const login = require('./views/login/index');//登录
const code = require('./views/code/index');//验证码

//注册
app.use(cors());//跨域
app.use(session({ secret: 'your-secret-key', resave: false, rolling: true, saveUninitialized: true, cookie: { maxAge: 1200000 } }));//20分钟
app.use(bodyParser.json()); // 在请求处理之前，先解析JSON  用来获取post参数`
app.use(express.urlencoded({ extended: true }));//解析提交的表单      这俩都要放路由之前

app.use(timeout('20s'));    //如果5s还没有响应，req.timedout将返回true  timedout没写错
app.use( (req, res, next)=> {
    if (!req.timedout) next();
});



app.use('/app', user, register, login, code);//注册路由

app.listen(3000, () => {
    console.log('服务已启动 http://localhost:3000/app      http://127.0.0.1:3000/app');
})

